<template>
  <div class="register">
    <div class="title">
      <p>注册未来校招账号</p>
    </div>
    <div class="tab-current">
      <div class="register-wrap">
        <div class="phone clearfix">
          <p>+0086</p>
          <input v-model="mobile" name="mobile" type="tel" placeholder="请输入手机号" />
        </div>
        <div class="code">
          <input v-model="msgCode" name="code" type="text" placeholder="请输入验证码" />
          <MsgCode :phone-number="mobile" />
        </div>
        <div class="pwd">
          <input v-model="password" name="password" type="password" placeholder="请设置密码" />
        </div>
      </div>
    </div>
    <div class="operate">
      <div class="auto">
        <label @click="toCheck">
          <span class="checkbox" v-show="checked">
            <i class="el-icon-check"></i>
          </span>
          <span class="checkbox" v-show="!checked">&nbsp;</span>
          <span class="text"
            >接受<nuxt-link target="_blank" to="/agreement"
              >《未来校招用户服务协议》</nuxt-link
            >
          </span>
        </label>
      </div>
    </div>
    <div class="submit">
      <button type="button" @click="submit">立即注册</button>
      <p @click="toLogin">已有账号？直接登录</p>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/users'
export default {
  data () {
    return {
      active: 0,
      checked: false,
      mobile: '',
      msgCode: '',
      password: '',
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    tab (active) {
      this.active = active;
    },
    toCheck () {
      this.checked = !this.checked;
    },
    toLogin () { // 登录
      this.$store.dispatch('user/toggleModal', {
        login: true
      })
    },
    submit () { // 注册
      const mobile = this.mobile || '';
      const msgCode = this.msgCode || '';
      const password = this.password || '';
      if (!mobile || !/^1[3456789]\d{9}$/.test(mobile)) {
        this.$message.warning('请正确填写手机号码！')
        return false
      }
      if (!msgCode) {
        this.$message.warning('请填写短信验证码！')
        return false
      }
      if (!password) {
        this.$message.warning('请填写密码！')
        return false
      }
      if (!this.checked) {
        this.$message.warning('请阅读服务协议后勾选接受！')
        return false
      }
      register({
        mobile,
        code: msgCode,
        password,
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg || '注册成功！');
          this.$store.dispatch('user/toggleModal', {
            identity: true
          })
        } else {
          this.$message.error(res.msg || '网络错误, 稍后再试！');
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误, 稍后再试！');
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.register {
  height: 430px;
  padding: 20px 50px;
  .title {
    height: 50px;
    p {
      line-height: 50px;
      text-align: center;
      font-size: 21px;
      color: #333;
    }
  }
  .register-wrap {
    margin: 20px 0;
    & > div {
      height: 55px;
      border-bottom: 1px solid #eee;
      input,
      p {
        line-height: 54px;
        height: 54px;
        color: #333;
        font-size: 16px;
      }
    }
    .phone {
      p {
        float: left;
        width: 80px;
      }
    }
  }
  .operate {
    .auto {
      label {
        .checkbox {
          display: inline-block;
          height: 22px;
          line-height: 21px;
          width: 22px;
          border: 1px solid #d8d8d8;
          border-radius: 50%;
          text-align: center;
          i {
            vertical-align: text-bottom;
            color: $base-color;
            font-size: 18px;
            font-weight: 600;
          }
        }
        .text,
        a {
          font-size: 16px;
          color: #999;
          margin-left: 5px;
        }
        &:hover {
          .text,
          a {
            color: $base-color;
            cursor: pointer;
          }
        }
      }
    }
  }
  .submit {
    margin-top: 40px;
    text-align: center;
    button {
      height: 45px;
      width: 335px;
      margin: 0 auto;
      background: $base-color;
      color: #fff;
      border: 1px solid $base-color;
      border-radius: 22.5px;
      font-size: 16px;
      &:hover {
        background: #fff;
        color: $base-color;
      }
    }
    p {
      margin-top: 10px;
      color: #999;
      font-size: 16px;
      text-align: center;
      &:hover {
        color: $base-color;
        cursor: pointer;
      }
    }
  }
}
</style>
